/**
 * Witontek.com.
 * Copyright (c) 2012-2017 All Rights Reserved.
 */
package test.xml;

import java.lang.reflect.Field;

/**
 * 
 * @author Long
 * @version $Id: Object2Xml.java, v 0.1 2017年2月19日 上午10:53:14  xiaolong.ma  Exp $
 */
public class Object2Xml {

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        WxPayRefundReq req = new WxPayRefundReq();
        req.setAppid("333");
        System.out.println(req.toXml());
    }

    public static String toXml(Object object) {
        Field[] fields = object.getClass().getDeclaredFields();
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>\n");
        for (Field field : fields) {
            Object obj;
            try {
                if (!"logger".equals(field.getName())) {
                    obj = field.get(object);
                    if (obj != null && !"".equals(obj)) {
                        sb.append("<").append(field.getName().trim()).append("><![CDATA[")
                            .append(obj).append("]]></").append(field.getName().trim())
                            .append(">\n");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sb.append("</xml>");
        return sb.toString();
    }

}
